// pages/detail/detail.js
const app = getApp();
const api = require('../../utils/backendAPI.js');

Page({

  data: {
    server: app.globalData.server,
    index: 0,
    writeComment: '写评价',
    checkboxes: {
      subContain: true,
      videoContain: true,
      operateContain: true,
      tipsContain: true,
      marksContain: true
    },
    operate: [{
      type: 'text',
      data: '暂无数据'
    }],
    show: null,
    tips: [{
      type: 'text',
      data: '暂无数据'
    }],
    comment: [],
    page: 0,
    avg: {
      jjxxd: 0,
      myd: 0,
      hdwt: 0,
      df: 0
    },
    max: {
      jjxxd: 0,
      myd: 0,
      hdwt: 0,
      df: 0
    },
    min: {
      jjxxd: 0,
      myd: 0,
      hdwt: 0,
      df: 0
    },
    subExp: [],
    subExpList: {
      1011: ["1010113 拉伸法测钢丝弹性模量",
              "1010212 扭摆法测定转动惯量"
            ],
      1021: ["1020114 光电效应法测定普朗克常数"],
      1022: ["1020314 稳态法测量不良导体热导率"],
      1031: ["1030113 模拟示波器的使用(必选)",
              "1030211 用模拟示波器测量二极管伏安特性",
              "1030312 用模拟示波器测量声速",
              "1030412 用数字示波器测量微积分流形"
            ],
      1041: ["1040114 伏安法测中电阻",
              "1040211 半偏法测检流计内阻与电流常数",
              "1040312 伏安法测高/低电阻",
              "1040412 惠斯通电桥测中电阻"
            ],
      1042: ["1040514 双电桥测低电阻",
              "1040522 双电桥改单电桥测中电阻"
            ],
      1051: ["1050114 自组电位差计测干电池电动势(必选)",
              "1050211 箱式电位差计测干电池电动势",
              "1050222 箱式电位差计测固定电阻",
              "1050232 箱式电位差计测电表内阻"
            ],
      1061: ["1060111 物距像距法测量凸透镜焦距",
              "1060213 自准直法测量透镜焦距",
              "1060312 共轭法测量凸透镜的焦距",
              "1060412 单球面镜焦距的测量",
            ],
      1062: ["1060513 平行光管法测量凸透镜焦距",
              "1060524 平行光管法测量凹透镜焦距"
            ],
      1071: ["1070113 分光仪的调整(必选)",
              "1070212 测量三棱镜的顶角(必选)",
              "1070312 最小偏向角法测量棱镜的折射率",
              "1070322 掠入射法测量棱镜的折射率",
              "1070412 平板玻璃折射率的测量",
              "1080225 钠光劳埃镜干涉"
            ],
      1081: ["1080114 弗兰克-赫兹实验",
            ],
      // 1082: ["1080215 钠光双棱镜干涉",
      //         "1080225 钠光劳埃镜干涉"
      //       ],
      1091: ["1090115 密立根油滴实验",
            ]
    }
  },

  onLoad: function (options) {
    this.data.subExp = this.data.subExpList[options.index];
    this.data.index = options.index;
    wx.setNavigationBarTitle({
      title: '实验' + options.index,
    })
    for (let key in this.data)
      if (this.data[key] == undefined)
        this.data[key] = "";
    this.setData(this.data)
  },
  onShow: function () {
    wx.showNavigationBarLoading()
    this.getComment()
    this.loadData()
    this.getCommentStat()
  },
  onPullDownRefresh: function () {
    this.getComment()
    this.loadData()
    this.getCommentStat()
  },
  onReachBottom: function () {
    if (this.data.comment.length >= (this.page + 1) * 10)
      this.moreComment()
  },
  onShareAppMessage: function (res) {
    return {
      title: '快来看看学长开发的万能基物实验小程序！',
      path: 'pages/index/index',
      imageUrl: 'https://s1.ax1x.com/2020/07/25/aSICY4.jpg',
      success: function (res) {
        console.log('成功分享', res)
      }
    }
  },
  // 由index向服务器请求，获取实验提示、步骤、演示
  loadData: function (data) {
    api.getExpDetail({ experiment: this.data.index }).then(res => {
      let obj = res.data
      if(typeof obj ==="string"){
        obj=JSON.parse(obj)
      }
      console.log(obj)
      this.setData({
        operate: obj.operate,
        tips: obj.tips,
        show: obj.show
      })
      wx.hideNavigationBarLoading()
    })
  },
  getCommentStat: function () {
    api.getCommentStat().then((res) => {
      console.log(res)
      let tmp_index = res.data.data.findIndex(x => x.experiment == this.data.index);
      if (tmp_index !== -1) {
        let result = res.data.data[tmp_index].data;
        this.data.avg.hdwt = Number(result[0]).toFixed(2)
        this.data.avg.myd = Number(result[1]).toFixed(2)
        this.data.avg.jjxxd = Number(result[2]).toFixed(2)
        this.data.avg.df = Number(result[3]).toFixed(2)
        this.data.min.hdwt = result[4]
        this.data.min.myd = result[5]
        this.data.min.jjxxd = result[6]
        this.data.min.df = result[7]
        this.data.max.hdwt = result[8]
        this.data.max.myd = result[9]
        this.data.max.jjxxd = result[10]
        this.data.max.df = result[11]
        for (let key in this.data)
          if (this.data[key] == undefined)
            this.data[key] = "";
        this.setData(this.data)
      }
    })
  },
  // 首次获取评论列表
  getComment: function (e) {
    this.data.page = 0
    api.getComment({
      experiment: this.data.index,
      page: this.data.page
    }).then(res => {
      let tmp_index = res.data.findIndex(x => x.isMine)
      if (tmp_index == -1)
        this.data.writeComment = '写评价'
      else
        this.data.writeComment = '编辑评价'
      this.data.comment = res.data.filter(x => x.comment.comment ? true : false)
      this.data.allComments = res.data
      for (let key in this.data)
        if (this.data[key] == undefined)
          this.data[key] = "";
      this.setData(this.data);
      wx.stopPullDownRefresh()
    })
  },
  // 获取更多评论
  moreComment: function (e) {
    this.data.page++
    api.getComment({
      experiment: this.data.index,
      page: this.data.page
    }).then(res => {
      this.data.comment = this.data.comment.concat(res.data)
      for (let key in this.data)
        if (this.data[key] == undefined)
          this.data[key] = "";
      this.setData(this.data);
      wx.stopPullDownRefresh()
    })
  },
  //折叠、显示容器卡片
  showContain: function (e) {
    let contain = e.currentTarget.dataset.contain
    let that = this
    that.data.checkboxes[contain] = !that.data.checkboxes[contain]
    that.setData({ checkboxes: that.data.checkboxes })
  },
  //跳转到评论发布页
  goCommentPub: function (e) {
    let tmp_index = this.data.allComments.findIndex(x => x.isMine)
    if (tmp_index == -1)
      wx.navigateTo({
        url: '../commentPub/commentPub?index=' + this.data.index,
      })
    else {
      wx.navigateTo({
        url: `/pages/commentPub/commentPub?index=${this.data.index}&editmode=1`,
      })
    }
  },
  //跳转到实验数据页
  goExpData: function (e) {
    wx.navigateTo({
      url: '../expData/' + this.data.index + '/' + this.data.index
    })
  },
  goDataReadme: function (e) {
    wx.navigateTo({
      url: '../dataReadme/dataReadme'
    })
  },
  //点击评论的Handler
  commentTap: function (e) {
    if (e.currentTarget.dataset.comment.isMine)
      wx.showActionSheet({
        itemList: ['编辑评价'],
        success: (result) => {
          if (result.tapIndex == 0) {
            wx.navigateTo({
              url: `/pages/commentPub/commentPub?index=${this.data.index}&editmode=1`,
            })
          }
        },
      })
  },
  //图片预览，图片点击的Handler
  previewImage: function (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.img],
    })
  },
})